package com.szit.test.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.szit.test.biz.CategoryBiz;
import com.szit.test.biz.impl.CategoryBizImpl;
import com.szit.test.entity.Category;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "ProvinceListServlet",urlPatterns = "/sys/listprovince")
public class CategoryListServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        // 调用逻辑层
        CategoryBiz pbiz = new CategoryBizImpl();
        List<Category> provinces = pbiz.list();

        //序列化为JSON格式的数据
        String jsonProvinces = JSON.toJSONStringWithDateFormat(provinces,"yyyy年MM月dd日 HH:mm", SerializerFeature.WriteMapNullValue);

        PrintWriter out = response.getWriter();
        out.print(jsonProvinces);
        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
